package com.hub.controller.asset;

import com.hub.controller.BaseController;
import com.hub.dto.asset.SearchSupplierInfoDto;
import com.hub.dto.asset.SupplierInfoDto;
import com.hub.frame.annotation.RestAuth;
import com.hub.utils.ApiCodeEnum;
import com.hub.utils.ApiResultObject;
import com.hub.utils.KeyValueModel;
import com.hub.utils.PagingResultObject;
import com.hub.utils.enums.SupplierTypeEnum;
import com.hub.service.asset.ISupplierInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author hub
 */
@RestController
@RequestMapping("/supplier")
public class SupplierInfoController extends BaseController {
    @Autowired
    private ISupplierInfoService supplierInfoService;

    @ApiOperation("分页查询")
    @PostMapping(value = "/list")
    public PagingResultObject getList(@RequestBody @Valid SearchSupplierInfoDto queryModel){
        return this.supplierInfoService.getList(queryModel);
    }

    @RestAuth(isPublic = true)
    @ApiOperation("获取供应商类型列表")
    @GetMapping(value = "/type/list")
    public ApiResultObject getSupplierTypeList(){
        List<KeyValueModel> slist = SupplierTypeEnum.getMaps();
        return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"",slist);
    }

    @ApiOperation("新增供应商")
    @PostMapping(value = "/add")
    public ApiResultObject doAddSupplier(@RequestBody @Valid SupplierInfoDto addModel, BindingResult bindingResult){
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }
        return this.supplierInfoService.doAddSupplier(addModel);
    }

    @ApiOperation("修改供应商")
    @PutMapping(value = "/{id}")
    public ApiResultObject doModifySupplier(@RequestBody @Valid SupplierInfoDto modifyModel, BindingResult bindingResult){
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }
        return this.supplierInfoService.doModifySupplier(modifyModel);
    }

    @ApiOperation("删除供应商")
    @DeleteMapping(value = "/{id}")
    public ApiResultObject doDeleteSupplier(@PathVariable int id){
        return this.supplierInfoService.doDeleteSupplier(id);
    }
}
